package qc.module.ehs.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import qc.common.core.enums.EhsRiskLevelEnum;
import qc.module.ehs.dto.troublecorrect.EhsTroubleCorrectInfoDto;
import qc.module.ehs.entity.EhsTroubleCorrect;

import java.util.List;

/**
 * EhsTroubleCorrectMapper
 * 
 * @author QuCheng Tech
 * @since 2024/11/7
 */
@Mapper
public interface EhsTroubleCorrectMapper {
    
    EhsTroubleCorrectMapper MAPPER = Mappers.getMapper(EhsTroubleCorrectMapper.class);
    
    @Mapping(target = "unit",source = "unitname")
    @Mapping(target = "tm", source = "tm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "intm", source = "intm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "reqendtm", source = "reqendtm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "correctendtm", source = "correctendtm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "checktm", source = "checktm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "risklevel", source = "risklevel.index")
    EhsTroubleCorrectInfoDto toDto(EhsTroubleCorrect en);
    
    List<EhsTroubleCorrectInfoDto> toDtoList(List<EhsTroubleCorrect> ens);

    @Mapping(target = "unitname",source = "unit")
    @Mapping(target = "tm", source = "tm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "intm", source = "intm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "reqendtm", source = "reqendtm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "correctendtm", source = "correctendtm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "checktm", source = "checktm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "risklevel", ignore = true)
    EhsTroubleCorrect toEntity(EhsTroubleCorrectInfoDto dto);

    default EhsTroubleCorrect dtoToEntity(EhsTroubleCorrectInfoDto dto) {
        EhsTroubleCorrect en = EhsTroubleCorrectMapper.MAPPER.toEntity(dto);

        en.setRisklevel(getEhsRiskLevelEnum(dto.getRisklevel()));

        return en;
    }

    static EhsRiskLevelEnum getEhsRiskLevelEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (EhsRiskLevelEnum item : EhsRiskLevelEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }

        //默认为null，风险等级不能为空
        return null;
    }
}
